package com.model;

import java.beans.Statement;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

public class Perpustakaan {

    private static String nama;
    public static ArrayList<Publikasi> DAFTAR_PUBLIKASI = new ArrayList<Publikasi>();
    public static ArrayList<Anggota> DAFTAR_ANGGOTA = new ArrayList<Anggota>();
    public static ArrayList<Pinjaman> DAFTAR_PINJAMAN = new ArrayList<Pinjaman>();

    public static String getNama() {
        return nama;
    }

    public static void setNama(String nama) {
        nama = nama;
    }

    public static void tambahPublikasi(Publikasi publikasi) {
        DAFTAR_PUBLIKASI.add(publikasi);
    }

    public static void tambahAnggota(Anggota anggota) {
        DAFTAR_ANGGOTA.add(anggota);
    }

    public static String bacaDaftarPublikasi() {
        if (DAFTAR_PUBLIKASI.isEmpty() == false) {
            String hasil = new String();
            for (int i = 0; i < DAFTAR_PUBLIKASI.size(); i++) {
                hasil += "Id Koleksi  : " + DAFTAR_PUBLIKASI.get(i).getIdKoleksi() + "\n";
                hasil += "Judul  : " + DAFTAR_PUBLIKASI.get(i).getJudul() + "\n";
                hasil += "Penerbit  : " + DAFTAR_PUBLIKASI.get(i).getPenerbit() + "\n";
                hasil += "Penulis  : " + DAFTAR_PUBLIKASI.get(i).getPenulis() + "\n";
                hasil += "Tahun  : " + DAFTAR_PUBLIKASI.get(i).getTahun() + "\n";
            }
            return hasil;
        }
        return "kosong";
    }

    public static String bacaDaftarAnggota() {
        if (DAFTAR_ANGGOTA.isEmpty() == false) {
            String hasil = new String();
            for (int i = 0; i < DAFTAR_ANGGOTA.size(); i++) {
                hasil += "Id Anggota  : " + DAFTAR_ANGGOTA.get(i).getIdAnggota() + "\n";
                hasil += "Nama  : " + DAFTAR_ANGGOTA.get(i).getNama() + "\n";
            }
            return hasil;
        }
        return "kosong";
    }

    public static void tambahPinjaman(Pinjaman pinjaman) {
        DAFTAR_PINJAMAN.add(pinjaman);
    }

    public static void simpanFilePublikasi() {
        FileOutputStream outStream = null;
        if (DAFTAR_PUBLIKASI.isEmpty()) {
            JOptionPane.showMessageDialog(null, "Daftar Publikasi Kosong");
        } else {
            File outFile = new File("publikasi.dat");
            try {
                outStream = new FileOutputStream(outFile);
            } catch (FileNotFoundException ex) {
                Logger.getLogger(Publikasi.class.getName()).log(Level.SEVERE, null, ex);
            }
            ObjectOutputStream outObjectStream = null;
            try {
                outObjectStream = new ObjectOutputStream(outStream);
            } catch (IOException ex) {
                Logger.getLogger(Publikasi.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                outObjectStream.writeObject(DAFTAR_PUBLIKASI);
            } catch (IOException ex) {
                Logger.getLogger(Publikasi.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                outStream.close();
            } catch (IOException ex) {
                Logger.getLogger(Publikasi.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public static void bacaFilePublikasi() {
        FileInputStream inStream = null;
        File inFile = new File("publikasi.dat");
        try {
            inStream = new FileInputStream(inFile);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Publikasi.class.getName()).log(Level.SEVERE, null, ex);
        }
        ObjectInputStream inObjectStream = null;
        try {
            inObjectStream = new ObjectInputStream(inStream);
        } catch (IOException ex) {
            Logger.getLogger(Publikasi.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            DAFTAR_PUBLIKASI = (ArrayList<Publikasi>) inObjectStream.readObject();
        } catch (IOException ex) {
            Logger.getLogger(Publikasi.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Publikasi.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void simpanFileAnggota() {
        FileOutputStream outStream = null;
        if (DAFTAR_ANGGOTA.isEmpty()) {
            JOptionPane.showMessageDialog(null, "Daftar Publikasi Kosong");
        } else {
            File outFile = new File("anggota.dat");
            try {
                outStream = new FileOutputStream(outFile);
            } catch (FileNotFoundException ex) {
                Logger.getLogger(Publikasi.class.getName()).log(Level.SEVERE, null, ex);
            }
            ObjectOutputStream outObjectStream = null;
            try {
                outObjectStream = new ObjectOutputStream(outStream);
            } catch (IOException ex) {
                Logger.getLogger(Publikasi.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                outObjectStream.writeObject(DAFTAR_ANGGOTA);
            } catch (IOException ex) {
                Logger.getLogger(Publikasi.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                outStream.close();
            } catch (IOException ex) {
                Logger.getLogger(Publikasi.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public static void bacaFileAnggota() {
        FileInputStream inStream = null;
        File inFile = new File("anggota.dat");
        try {
            inStream = new FileInputStream(inFile);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Publikasi.class.getName()).log(Level.SEVERE, null, ex);
        }
        ObjectInputStream inObjectStream = null;
        try {
            inObjectStream = new ObjectInputStream(inStream);
        } catch (IOException ex) {
            Logger.getLogger(Publikasi.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            DAFTAR_ANGGOTA = (ArrayList<Anggota>) inObjectStream.readObject();
        } catch (IOException ex) {
            Logger.getLogger(Publikasi.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Publikasi.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
     public static void simpanTablePublikasi() {
        if (DAFTAR_PUBLIKASI.isEmpty()) {
            JOptionPane.showMessageDialog(null, "Daftar Publikasi Kosong");
        } else {
            Connection conn = OraConnection.open("jdbc:oracle:thin:@127.0.0.1:1521:xe", "system", "115314087");
           
            try {
                java.sql.Statement statement = conn.createStatement();
                for (int i = 0; i < DAFTAR_PUBLIKASI.size(); i++) {
                    String sql = "insert into publikasi (id_koleksi, judul) values ('" + DAFTAR_PUBLIKASI.get(i).getIdKoleksi() + "','" + DAFTAR_PUBLIKASI.get(i).getJudul() + "')";
                    statement.executeUpdate(sql);
                }
                conn.close();
            } catch (SQLException ex) {
                Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public static void bacaTabelPublikasi() throws SQLException {
        DAFTAR_PUBLIKASI = new ArrayList<Publikasi>();
        Connection conn = OraConnection.open("jdbc:oracle:thin:@127.0.0.1:1521:xe", "system", "115314023");
        java.sql.Statement statement = conn.createStatement();
        String sql = "select * from publikasi";
        ResultSet result = statement.executeQuery(sql);

        while (result.next()) {
            Publikasi temp = new Publikasi() {

                @Override
                public int hitungDenda(int lamaPinjam) {
                    throw new UnsupportedOperationException("Not supported yet.");
                }
            };
            try {
                temp.setIdKoleksi(result.getString("id_koleksi"));
            } catch (Exception ex) {
                Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
            }
            temp.setJudul(result.getString("judul"));
            DAFTAR_PUBLIKASI.add(temp);
        }
        conn.close();
    }

    public static void tambahTabelPublikasi(Publikasi publikasi) {
        try {
            Connection conn = OraConnection.open("jdbc:oracle:thin:@127.0.0.1:1521:xe", "System", "115314087");
            java.sql.Statement statement = conn.createStatement();
            String sql = "insert into publikasi (id_koleksi, judul) values ('" + publikasi.getIdKoleksi() + "','" + publikasi.getJudul() + "')";
            statement.executeUpdate(sql);
            conn.close();
        } catch (SQLException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static ArrayList<Anggota> cariAnggota(String nama) {
        ArrayList<Anggota> hasil = new ArrayList<Anggota>();
        for (int i = 0; i < DAFTAR_ANGGOTA.size(); i++) {
            Anggota temp = DAFTAR_ANGGOTA.get(i);
            if (temp.getNama().contains(nama)) {
                hasil.add(temp);
            }
        }
        return hasil;
    }
}
